#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;

class A
{
public:
  A(int n)
    :_a(new int[n])
  {
    cout << "A(int a)" << endl; 
  }

  ~A()
  {
    delete[] _a;
    cout << "~A()" << endl;
  }
private:
  int* _a;
};

int main()
{
  while (1){
    cout << " I am a process, pid is " << getpid() << endl;
    sleep(1);
  }
  return 0;
}
int xmain()
{
  pid_t id = fork();
  if (id < 0){
    cerr << "fork error" << endl;
  }
  else if (id == 0){
    // child
    int count = 5;
    while (count){
      printf("child[%d]:I am running... count:%d\n", getpid(), count--);
      sleep(1);
    }
    exit(0);
  }
  // 基于阻塞的轮询等待
  // parent
  //while (1){
  //  int status;
  //  pid_t ret = waitpid(-1, &status, WNOHANG);
  //  if (ret == 0){
  //    // 子进程还未结束
  //    printf("father is running...\n");
  //    sleep(1);
  //  }
  //  else if (ret > 0){
  //    // 子进程退出
  //    if (WIFEXITED(status)){
  //      // 正常退出
  //      printf("child success exited, exit code is:%d\n", WEXITSTATUS(status));
  //    }
  //    else{
  //      // 异常退出
  //      printf("child exited error,exit singal is:%d", status&0x7f);
  //    }
  //    break;
  //  }
  //  else{
  //    printf("wait child failed\n");
  //    break;
  //  }
  //}
  // 阻塞等待
  // parent
  printf("father begins waiting...\n");
  int status;
  pid_t ret = waitpid(id, &status, 0);
  printf("father finish waiting...\n");

  if (id > 0 && WIFEXITED(status)){
    // 正常退出
    printf("child success exited, exit code is:%d\n", WEXITSTATUS(status));
  }
  else if (id > 0){
    // 异常退出
    printf("child exit failed,core dump is:%d,exit singal is:%d\n", (status&(1<<7)), status&0x7f);
  }
  else{
    printf("father wait failed\n");
  }
}
int in()
{
  pid_t ret = fork();
  if (ret < 0){
    cerr << "fork error" << endl;
  }
  else if (ret == 0){
    // child
    int count = 5;
    while (count){
      printf("child[%d]:I am running... count:%d\n", getpid(), count--);
      sleep(1);
    }
    //int a = 10;
    //int b = 0;
    //a /= b;
    exit(1);
  }
  // parent
  printf("father begins waiting...\n");
  //sleep(10);
  //pid_t id = wait(NULL);// 不关心子进程退出状态
  int status;
  pid_t id = wait(&status);// 从status中获取子进程退出的状态信息
  //pid_t ret = waitpid(-1/*-1代表等待任意进程，和wait效果一致，大于0代表等待的子进程PID*/, &status, 0/*0代表以阻塞方式等待，WNOHANG代表检测一次，然后不予于等待*/);
  // pid_t ret = waitpid(id, &status, 0);
  printf("father finish waiting...\n");

  if (id > 0 && (status&0x7f) == 0){
    // 正常退出
    printf("child success exited, exit code is:%d\n", (status>>8)&0xff);
  }
  else if (id > 0){
    // 异常退出
    printf("child exit failed,core dump is:%d,exit singal is:%d\n", (status&(1<<7)), status&0x7f);
  }
  else{
    printf("father wait failed\n");
  }
  if (id > 0){ 
    printf("child success exited\n"); 
  } else{
    printf("child exit failed\n"); 
  } 
  //父进程再活5秒 
  sleep(5);
  return 0;
}

int fn()
{
  //exit(0);
  //_exit(0);
  
  return 1;
}

int ss()
{
  cout << "12345";
  sleep(3);
   exit(0);// 退出进程前前会执行用户定义的清理函数，且刷新缓冲区
  // _exit(0);// 直接退出进程
  return 0;
}
